package com.gxa.community.customer.service.impl;


import com.alibaba.fastjson.JSON;
import com.gxa.community.core.common.CommonVar;
import com.gxa.community.core.common.RequestUrl;
import com.gxa.community.core.pojo.dto.customer.dto.UserAgentApplicationDto;
import com.gxa.community.core.utils.R;
import com.gxa.community.customer.service.UserService;
import com.gxa.community.customer.util.RestUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl implements UserService {

    final
    RedisTemplate<String,Object> redisTemplate;
    public UserServiceImpl(RedisTemplate<String, Object> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    //Do 团长申请
    @Override
    public R addUserApplicationAgent(UserAgentApplicationDto userAgentApplicationDto) {
        //从数据库中后台中取出数据
        String url = RequestUrl.ADMIN_URL + "/agent/agent/application";

        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("userAgentApplicationDto", JSON.toJSONString(userAgentApplicationDto));

        return RestUtil.postWithParams(url, params);
    }

    //DO 获取用户信息
    @Override
    public R getUserInfo(String token) {
        Object o =  redisTemplate.opsForValue().get(CommonVar.USER_LOGIN_AXLAB_TOKEN.getKeyWord() + token);
        return !ObjectUtils.isEmpty(o) ? R.ok().put("data", o) : R.error("获取用户信息失败");
    }

    //Do 退出登录
    @Override
    public R loginOut(String token) {
        //从数据库中后台中取出数据
        String url = RequestUrl.SSO_URL.getUrl() + "/user/loginOut";
        MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
        params.add("token", token);
        return RestUtil.postWithParams(url, params);
    }

}
